package com.zj.core.dialog

import android.app.Activity
import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.zj.core.R
import com.zj.core.adapter.ListFormAdapter
import com.zj.core.tools.ViewPosition
import com.zj.core.base.BaseApplication
import com.zj.core.base.BaseDialog
import com.zj.core.interfaces.TextMessageIml
import com.zj.core.ui.ListFormDialogUi
import kotlin.collections.ArrayList

/**
 * Created by Administrator on 2019/5/23.
 */

@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
class ListFormDialog(context: Activity?) : BaseDialog(context), View.OnClickListener {
    private var mTemplate: ListFormAdapter? = null
    private var callBackIml: TextMessageIml? = null
    private var mFormList: ArrayList<String>? = ArrayList()
    private var mOldFormList:List<String>?= ArrayList()
    private var mLayoutManager: LinearLayoutManager?=null
    private var rootView: ListFormDialogUi?=null
    private var mFormValueStr:String?=""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val width: Int = ViewPosition().getScreenWidth(this.context)
        val height: Int = (ViewPosition().getScreenHeight(this.context) * 0.95).toInt()
        val params = LinearLayout.LayoutParams(width, height, 0f)

        rootView= ListFormDialogUi(context, this)
        rootView?.apply {
            setContentView(common_dialog_root_view, params)

            //setCancelable(cancelable)
            setCanceledOnTouchOutside(true)

            mLayoutManager = LinearLayoutManager(BaseApplication.context)
            mLayoutManager?.orientation = LinearLayoutManager.VERTICAL

            mTemplate = ListFormAdapter(context, mFormList, mOldFormList)
            template_list_rv?.layoutManager = mLayoutManager
            template_list_rv?.adapter = mTemplate
        }
    }

    fun setCallBackIml(callBackIml: TextMessageIml): ListFormDialog {
        this.callBackIml = callBackIml
        return this
    }

    fun setFormListValue(formList: ArrayList<String>): ListFormDialog {
        this.mFormList = formList
        return this
    }

    fun setOldFormStr(formList: List<String>): ListFormDialog {
        this.mOldFormList=formList
        return this
    }

    override fun onClick(v: View?) {
        mFormValueStr=""
        when (v?.id) {
            R.id.common_dialog_cancel_tv -> dismiss()

            R.id.common_dialog_confirm_tv ->{
                for(form in 0 until rootView?.template_list_rv!!.childCount){
                    val mConstraint:ConstraintLayout = rootView?.template_list_rv!!.getChildAt(form) as ConstraintLayout
                    val mImg=mConstraint.getChildAt(1) as ImageView
                    val mText=mConstraint.getChildAt(2) as TextView
                    if(mImg.tag==2){
                        mFormValueStr = if(mFormValueStr==""){
                            mText.text.toString()
                        }else{
                            mFormValueStr+","+mText.text
                        }
                    }
                }
                callBackIml?.messageResult(mFormValueStr!!)
                dismiss()
            }
        }
    }

    override fun dismiss() {
        super.dismiss()
        if(mTemplate!=null){
            mTemplate=null
        }
        if(mFormList!=null){
            mFormList?.clear()
            mFormList=null
        }

        if(mLayoutManager!=null){
            mLayoutManager=null
        }
    }
}
